package vip.xiaozhao.paotui.controller.detail;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaozhao.paotui.controller.BaseController;
import vip.xiaozhao.paotui.intf.dto.ResponseDO;
import vip.xiaozhao.paotui.intf.entity.PaotuiDetail;
import vip.xiaozhao.paotui.intf.entity.PaotuiDetailAudit;
import vip.xiaozhao.paotui.intf.enums.*;
import vip.xiaozhao.paotui.intf.service.PaotuiAuditService;
import vip.xiaozhao.paotui.intf.service.PaotuiDetailService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/detail/issue")
@Slf4j
public class PaotuiDetailController extends BaseController {


    @Resource
    private PaotuiDetailService paotuiDetailService;
    @Resource
    private PaotuiAuditService paotuiAuditService;

    //详情数据接口【包含第二期的等待抢单、第三期的两个状态的状态查询】
    @RequestMapping("/loadById")
    public ResponseDO loadById(int paotuiId , HttpServletRequest request, HttpServletResponse response) {
        log.info("load paotui  , id =: "  + paotuiId);

        if(paotuiId <= 0){
            ResponseDO.fail("请求参数不正确");
        }
        PaotuiDetail detail = paotuiDetailService.loadById(paotuiId);
        if(detail == null){
            ResponseDO.fail("请求参数不正确");
        }
        //业务较多，且结果后续不断增加时，使用map
        HashMap<String, Object> map = new HashMap<String, Object>();

        //这里面有两个状态：1个是任务的状态，第2个是用户自己的状态
        PaotuiStatusEnum paotuiStatusEnum = PaotuiStatusEnum.getById(detail.getStatus());

        if(paotuiStatusEnum == PaotuiStatusEnum.WaitPao ){ //等待接单中
            // 1 取货信息打码，不能交给前端, 这个不实现
            map.put("detail", detail);
        }else if(paotuiStatusEnum == PaotuiStatusEnum.JieDan){//接单确认中，这个状态只有接单人可以从查询接口拉数据
            // 判断是不是本人，如果不是的话，返回错误接口
            //必须要登录
            int currentUserId = getCurrentUserId(request);
            if (currentUserId < 0) {
                return new ResponseDO(false, NOT_LOGIN, null);
            }
            PaotuiDetailAudit detailAudit = paotuiAuditService.loadByUserIdAndGoingStatus(currentUserId, paotuiId, PaotuiGoingStatusEnum.YiJieDan);

            if(detailAudit == null){
               return  ResponseDO.fail("用户接单数据不正确");
            }

            map.put("detail", detail);
            map.put("goingStatus", PaotuiGoingStatusEnum.YiJieDan.getId());
        } else if(paotuiStatusEnum == PaotuiStatusEnum.Confirmed){//送货过程中,暂时逻辑与接单数据一致，但是因为后续会增加较多的逻辑，这里分开
            int currentUserId = getCurrentUserId(request);
            if (currentUserId < 0) {
                return new ResponseDO(false, NOT_LOGIN, null);
            }
            PaotuiDetailAudit detailAudit = paotuiAuditService.loadLatestByUserIdAndPaotuiId(currentUserId, paotuiId);

            if(detailAudit == null){
                return  ResponseDO.fail("用户接单数据不正确");
            }
            map.put("detail", detail);
            map.put("goingStatus", detailAudit.getStatus());
        }else{  //已结束和已评价，已评价后续也会区分出来
            map.put("detail", detail);
        }

        return ResponseDO.success(map);
    }

    //用户抢单接口 ,只允许等待抢单和已签单状态
    //每10秒所有用户一个轮询，该接口压力较大, 只取关键字段
    @RequestMapping("/toOrder")
    public ResponseDO toOrder(int paotuiId , HttpServletRequest request, HttpServletResponse response) {

        //参数判空， n
        int currentUserId = getCurrentUserId(request);
        if (currentUserId < 0) {
            return new ResponseDO(false, NOT_LOGIN, null);
        }

        PaotuiDetail detail = paotuiDetailService.loadVersionLockById(paotuiId);
        if(detail == null){
            ResponseDO.fail("请求参数不正确");
        }

        if(detail.getStatus() == PaotuiStatusEnum.WaitPao.getId()){
            //抢单成功 ,乐观锁
            int ret = paotuiDetailService.updateByVersionLock(detail);
            if(ret == 0){
                return ResponseDO.fail("等待抢单");
            }else{
                //audit
                PaotuiDetailAudit audit = new PaotuiDetailAudit();
                audit.setStatus(PaotuiGoingStatusEnum.YiJieDan.getId());
                //其它set
                paotuiAuditService.insert(audit);

                detail.setStatus(PaotuiStatusEnum.JieDan.getId());
                return ResponseDO.success(paotuiId);
            }

        }

        return ResponseDO.success(detail);
    }


    //插入流水
    @RequestMapping("/setStatus")
    public ResponseDO setStatus(int paotuiId , int goingStatus,  HttpServletRequest request, HttpServletResponse response) {
        //参数判空， n
        int currentUserId = getCurrentUserId(request);
        if (currentUserId < 0) {
            return new ResponseDO(false, NOT_LOGIN, null);
        }

        PaotuiDetailAudit audit = new PaotuiDetailAudit();
        audit.setStatus(PaotuiGoingStatusEnum.YiJieDan.getId());
        //其它set
        paotuiAuditService.insert(audit);

        //如果是到了结束状态，需要同时更新主表的状态

        return ResponseDO.success(paotuiId);
    }



}
